﻿using MediatR;
using SystemMHB.API.Application.Command.ReturnWarehouseCommand;
using SystemMHB.Domian;
using SystemMHB.Domian.DTO;
using SystemMHB.Infrstructer.Interface;

namespace SystemMHB.API.Application.CommandHandlers.ReturnWarehouseCommandHandler
{
    public class ReturnWarehouseShowCommandHandler:IRequestHandler<ReturnWarehouseShowCommand, PagingDTO<ReturnWarehouse>>
    {
        /// <summary>
        /// 返库信息
        /// </summary>
        IReturnWarehouseRepository repository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository"></param>
        public ReturnWarehouseShowCommandHandler(IReturnWarehouseRepository repository)
        {
            this.repository = repository;
        }

        public async Task<PagingDTO<ReturnWarehouse>> Handle(ReturnWarehouseShowCommand request, CancellationToken cancellationToken)
        {
            var paging = new PagingDTO<ReturnWarehouse>();
            var list = (from retW in await repository.GetAllAsync()
                        where retW.IsDel == false
                        select retW);
            if (!string.IsNullOrEmpty(request.outboundNumber))
            {
                list = list.Where(p => p.outboundNumber.Contains(request.outboundNumber));
            }
            if (!string.IsNullOrEmpty(request.AddTime))
            {
                var Kdate = Convert.ToDateTime(request.AddTime).Date;
                var JDate = Kdate.AddDays(1).Date;
                list = list.Where(p => p.AddTime > Kdate & p.AddTime < JDate);
            }
            if (request.returnStatus != 0)
            {
                list = list.Where(p => p.returnStatus == request.returnStatus);
            }
            paging.TotaCount = list.Count();
            paging.pageCount = (int)Math.Ceiling((paging.TotaCount * 1.0) / request.pageSzen);
            paging.plist = list.OrderBy(p => p.rWId).Skip((request.pageiden - 1) * request.pageSzen).Take(request.pageSzen).ToList();
            return paging;

        }
    }
}
